package com.deep.bilibili.controller;


import cn.hutool.core.date.DateUtil;
import com.deep.bilibili.config.LiveConfig;
import com.deep.bilibili.constant.SRSConstant;
import com.deep.bilibili.dao.LiveDao;
import com.deep.bilibili.dto.LiveDto;
import com.deep.bilibili.repository.LiveRepository;
import com.deep.bilibili.util.LiveUtil;
import com.deep.bilibili.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.*;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/admin/live")
public class LiveController {
    private static Logger logger = LoggerFactory.getLogger(LiveController.class);

    @Autowired
    LiveRepository liveRepository;

    @Autowired
    LiveConfig liveConfig;




    /**
     * 观看页面
     *
     * @param modelMap
     * @return
     */
    @GetMapping("webWatch")
    public ModelAndView webWatch(ModelMap modelMap) {
        return new ModelAndView("live/watch", modelMap);
    }


    /**
     * 直播页面
     *
     * @param modelMap
     * @return
     */
    @GetMapping("webSrs")
    public ModelAndView webSrs(ModelMap modelMap) {
        modelMap.addAttribute("txt_url", "webrtc://srs.server/live/livestream");
        return new ModelAndView("live/srs", modelMap);
    }

    /**
     * 直播页面
     *
     * @param modelMap
     * @return
     */
    @GetMapping("webBroadcast")
    public ModelAndView webBroadcast(ModelMap modelMap) {
        return new ModelAndView("live/broadcast", modelMap);
    }


    /**
     * 直播列表
     * @param modelMap
     * @return
     */
    @GetMapping("webSrsLives")
    public ModelAndView webSrsLives(ModelMap modelMap) {
        List<LiveDao> lives = liveRepository.findAll();
        System.out.println(lives);
        modelMap.addAttribute("lives", lives);
        return new ModelAndView("live/srs_lives", modelMap);
    }


    /**
     * 直播推流
     * @param modelMap
     * @param id
     * @return
     */
    @GetMapping("webSrsPush")
    public ModelAndView webSrsPush(ModelMap modelMap, @RequestParam Long id) {
        System.out.println(id);

        LiveDao liveDO = liveRepository.getOne(id);
        System.out.println(liveDO);
        modelMap.addAttribute("live", liveDO);

        String path = liveDO.getPath();
        String pushUrl = liveDO.getRtcUrl();

        modelMap.addAttribute("pushUrl", pushUrl);

        Integer autostart = 0;

        modelMap.addAttribute("autostart", autostart);

        return new ModelAndView("live/srs_push", modelMap);
    }

    /**
     * 直播播放
     * @param modelMap
     * @param id
     * @return
     */
    @GetMapping("webSrsPlay")
    public ModelAndView webSrsPlay(ModelMap modelMap, @RequestParam Long id) {
        LiveDao liveDO = liveRepository.getOne(id);
        System.out.println(liveDO);
        modelMap.addAttribute("live", liveDO);

        String path = liveDO.getPath();
        String rtmpUrl = SRSConstant.RTMP_DOMAIN + path;
        String rtcUrl = SRSConstant.RTC_DOMAIN + path;

        modelMap.addAttribute("rtmpUrl", rtmpUrl);
        modelMap.addAttribute("rtcUrl", rtcUrl);
        modelMap.addAttribute("playUrl", rtmpUrl);

        Integer autostart = 0;

        modelMap.addAttribute("autostart", autostart);

        return new ModelAndView("live/srs_play", modelMap);
    }

    /**
     * smtp 播放
     * @param modelMap
     * @param id
     * @return
     */
    @GetMapping("webSrsRtmp")
    public ModelAndView webSrsRtmp(ModelMap modelMap, @RequestParam Long id) {
        LiveDao liveDO = liveRepository.getOne(id);
        System.out.println(liveDO);
        modelMap.addAttribute("live", liveDO);

        String path = liveDO.getPath();
        String rtmpUrl = SRSConstant.RTMP_DOMAIN + path;
        String rtcUrl = SRSConstant.RTC_DOMAIN + path;

        modelMap.addAttribute("rtmpUrl", rtmpUrl);
        modelMap.addAttribute("rtcUrl", rtcUrl);
        modelMap.addAttribute("playUrl", rtmpUrl);

        Integer autostart = 0;

        modelMap.addAttribute("autostart", autostart);

        return new ModelAndView("live/srs_rtmp", modelMap);
    }

    /**
     * 添加直播
     * @param liveDto
     * @return
     */
    @PostMapping("webSrsAdd")
    @ResponseBody
    public Object webSrsPlay(@RequestBody LiveDto liveDto) {
        String path = liveDto.getPath();
        String liveno = liveDto.getLiveno();

        LiveDao liveDO = new LiveDao();
        liveDO.setUid(1L);
        liveDO.setPath(path);
        liveDO.setLiveno(liveno);

        String rtmpUrl = LiveUtil.getRtmpUrl(path);
        String rtcUrl = LiveUtil.getWebrtcUrl(path);

        liveDO.setRtcUrl(rtcUrl);
        liveDO.setRtmpUrl(rtmpUrl);
        liveDO.setDownUrl("");

        String datetime = DateUtil.format(new Date(), "yyyy/MM/dd HH:mm:ss");
        liveDO.setAddTime(datetime);
        liveDO.setUpdateTime(datetime);

        liveRepository.save(liveDO);

        return ResponseUtil.ok();
    }

    /**
     * 直播停止
     * @param response
     * @param id
     * @throws IOException
     */
    @GetMapping("webSrsStop")
    public void webSrsStop(HttpServletResponse response, @RequestParam Long id) throws IOException {
        LiveDao liveDao = liveRepository.getOne(id);
        System.out.println(liveDao);

        liveRepository.delete(liveDao);

        response.sendRedirect("/live/webLives");
    }

}
